package com.yunji.web.base;

import com.yunji.aggregate.model.dto.base.AgentParam;
import com.yunji.aggregate.model.dto.base.SiteParam;
import com.yunji.aggregate.model.vo.base.AgentVO;
import com.yunji.aggregate.model.vo.base.SiteStatisticsVO;
import com.yunji.aggregate.service.BaseAggregateService;
import com.yunji.base.business.AgentBusiness;
import com.yunji.base.dto.AgentDTO;
import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * @version 1.0
 * @Project: yunji-coffee
 * @Package com.yunji.web.user
 * @FileName MangerController.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * Company		com
 * @CreateTime 2024-04-08 13:50:12
 * @Description 代理商交互
 * @Modification Author                    History Time			Version				Description
 * --------------------------------------------------------------------------------
 * HuaAo				2024-04-08 13:50:12		1.0					1.0 Version
 * @since JDK 1.8.0_202
 */
@RestController
@Api(tags = "管理端-代理商模块")
@RequestMapping("/container/agent")
public class AgentController extends BaseController {
    @Resource
    private AgentBusiness business;

    @Resource
    private BaseAggregateService aggregate;

    @ApiOperation("获取代理商用户列表")
//    @PreAuthorize("@ss.hasPermi('container:agent:query')")
    @GetMapping("/list")
    public Response<List<AgentVO>> list(@Valid AgentParam agentParam) {
        List<AgentVO> list = aggregate.agentList(agentParam);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出代理商用户列表")
    @PreAuthorize("@ss.hasPermi('container:agent:export')")
    @PostMapping("/export")
    public void export(AgentParam agentParam) {
        List<AgentVO> list = aggregate.agentList(agentParam);
        ExcelUtil<AgentVO> util = new ExcelUtil<>(AgentVO.class);
        util.exportExcel(response, list, "代理商用户");
    }

    @ApiOperation("新增代理商")
    @PreAuthorize("@ss.hasPermi('container:agent:add')")
    @Log(title = "新增代理商", businessType = BusinessType.INSERT)
    @PostMapping
    public Response<Long> add(@RequestBody AgentDTO agentDTO) {
        return ResponseHelper.success(business.create(agentDTO));
    }

    @ApiOperation("编辑代理商")
    @PreAuthorize("@ss.hasPermi('container:agent:edit')")
    @Log(title = "编辑代理商", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response<String> edit(@RequestBody AgentDTO dto) {
        business.update(dto);
        return ResponseHelper.success("修改成功");
    }

    @ApiOperation("删除代理商")
    @PreAuthorize("@ss.hasPermi('container:agent:remove')")
    @DeleteMapping("/{id}")
    public Response<String> remove(@PathVariable Long id) {
        business.delete(id);
        return ResponseHelper.success("删除成功");
    }

    @ApiOperation("代理商详情")
    @GetMapping(value = "/{id}")
    public Response<AgentDTO> getInfo(@PathVariable("id") Long id) {
        return ResponseHelper.success(business.select(id));
    }

    @ApiOperation("代理商可绑定网点")
    @GetMapping("/usable")
    public Response<List<SiteStatisticsVO>> usableSites() {
        List<SiteStatisticsVO> list = aggregate.siteList();
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("代理商下的网点信息")
    @GetMapping("/sitesInfo")
    public Response<List<SiteStatisticsVO>> sitesInfo(SiteParam param) {
        List<SiteStatisticsVO> list = aggregate.siteList(param);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("获取代理分润比例")
    @GetMapping("/giveAwayLimit")
    public Response<BigDecimal> getAgentScaleRatio() {
        return ResponseHelper.success(business.getAgentScaleRatio());
    }

    @ApiOperation("获取当前登录代理的信息")
    @GetMapping("/getAgentInfo")
    public Response<AgentDTO> getAgentInfo() {
        return ResponseHelper.success(business.getAgentInfo());
    }

    /**
     * ------------------------------------------搜索栏------------------------------------------
     */
    @ApiOperation("用户昵称匹配搜索")
    @GetMapping("/searchName")
    public Response<List<String>> searchName(@RequestParam("name") String name) {
        return ResponseHelper.successWithPage(business.searchName(name));
    }

    @ApiOperation("手机号码匹配搜索")
    @GetMapping("/searchPhone")
    public Response<List<String>> searchPhone(@RequestParam("phone") String phone) {
        return ResponseHelper.successWithPage(business.searchPhone(phone));
    }

}
